package com.example.hilt_mvvm_http

import com.example.hilt_mvvm_http.app.appContext
import com.example.hilt_mvvm_http.one.Api
import com.example.hilt_mvvm_http.one.HttpHeaderInterceptor
import com.example.hilt_mvvm_http.two.HttpMallHeaderInterceptor
import com.example.hilt_mvvm_http.two.MallApi

import dagger.Module
import dagger.Provides
import dagger.hilt.InstallIn
import dagger.hilt.components.SingletonComponent
import okhttp3.Cache
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.io.File
import javax.inject.Inject
import javax.inject.Qualifier
import javax.inject.Singleton
import okhttp3.logging.HttpLoggingInterceptor as HttpLoggingInterceptor1

/***
 * 网络注入模块
 */
@InstallIn(SingletonComponent::class)
@Module
object NetModule {
    private const val DEFAULT_TIME_OUT = 60L
    var cache: Cache = Cache(File("${appContext.externalCacheDir?.path}/okhttp_cache/"), 100 * 1024 * 1024)

    /**
     * 提供OkhttpClient
     */
    @Singleton
    @Provides
    @CommonOkhttp
    fun provideOkhttpClient():OkHttpClient{
        val httpLoggingInterceptor = HttpLoggingInterceptor1()
        //判断是否debug模式下
        if(BuildConfig.DEBUG){
            httpLoggingInterceptor.level= HttpLoggingInterceptor.Level.BODY
        }
        return OkHttpClient.Builder()
            .addInterceptor(HttpHeaderInterceptor())//自定义请求头拦截器
            .addInterceptor(httpLoggingInterceptor)
            .retryOnConnectionFailure(true)//失败重连
            .cache(cache)//缓存
            .sslSocketFactory(//https证书认证
                SSLSocketFactoryUtil.createSSLSocketFactory(),
                SSLSocketFactoryUtil.createTrustAllManager()
            )
            .hostnameVerifier(SSLSocketFactoryUtil.TrustAllHostnameVerifier())//域名认证
            .build()
    }

    /**
     * 提供retrofit
     */
    @Singleton
    @Provides
    @CommonRetrofit
    fun provideRetrofit(@CommonOkhttp okHttpClient: OkHttpClient):Retrofit{
        val retrofit = Retrofit.Builder()
            .baseUrl(BuildConfig.API_URL)
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
        return  retrofit
    }


    @Singleton
    @Provides
    fun provideApiService(@CommonRetrofit retrofit: Retrofit): Api {
        return retrofit.create(Api::class.java)
    }



    /**
     * 提供商城OkhttpClient
     */
    @Singleton
    @Provides
    @MallApiOkhttp
    fun provideMallOkhttpClient():OkHttpClient{
        val httpLoggingInterceptor = HttpLoggingInterceptor()
        //判断是否debug模式下
        if(BuildConfig.DEBUG){
            httpLoggingInterceptor.level= HttpLoggingInterceptor.Level.BODY
        }

        return OkHttpClient.Builder()
            .addInterceptor(HttpMallHeaderInterceptor())//自定义请求头拦截器
            .addInterceptor(httpLoggingInterceptor)
            .retryOnConnectionFailure(true)//失败重连
            .cache(cache)//缓存
            .sslSocketFactory(//https证书认证
                SSLSocketFactoryUtil.createSSLSocketFactory(),
                SSLSocketFactoryUtil.createTrustAllManager()
            )
            .hostnameVerifier(SSLSocketFactoryUtil.TrustAllHostnameVerifier())//域名认证
            .build()
    }

    /**
     * 提供商城retrofit
     */
    @Singleton
    @Provides
    @MallApiRetrofit
    fun provideMallRetrofit(@MallApiOkhttp okHttpClient: OkHttpClient):Retrofit{
        val retrofit = Retrofit.Builder()
            .baseUrl(BuildConfig.MALL_API_URL)
            .client(okHttpClient)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
        return  retrofit
    }

    @Singleton
    @Provides
    fun provideMallApiService(@MallApiRetrofit retrofit: Retrofit): MallApi {
        return retrofit.create(MallApi::class.java)
    }



    /**使用相应的限定符为字段或参数添加注解来注入所需的特定类型*/

    @Qualifier
    @Retention(AnnotationRetention.BINARY)
    annotation class CommonRetrofit

    //对接数引网Retrofit
    @Qualifier
    @Retention(AnnotationRetention.BINARY)
    annotation class MallApiRetrofit


    @Qualifier
    @Retention(AnnotationRetention.BINARY)
    annotation class CommonOkhttp

    //对接数引网okhttp
    @Qualifier
    @Retention(AnnotationRetention.BINARY)
    annotation class MallApiOkhttp


}